package com.tuyue.police.biz.impl;

import com.tuyue.police.WebSocked.MyWebSocked;
import com.tuyue.police.bean.MessageBean;
import com.tuyue.police.biz.IDispatcherBiz;
import com.tuyue.police.biz.IPoliceInfoBiz;
import com.tuyue.police.dao.IPoliceInfoDao;
import com.tuyue.police.dao.ISectorDao;
import com.tuyue.police.dao.IUserDao;
import com.tuyue.police.enums.ResultEnum;
import com.tuyue.police.exception.MyException;
import com.tuyue.police.pojo.PoliceInfo;
import com.tuyue.police.pojo.Sector;
import com.tuyue.police.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2018/3/16.
 * @Modified By:
 */
@Service
public class PoliceInfoBizImpl implements IPoliceInfoBiz {

    @Autowired
    private IPoliceInfoDao policeInfoDao;

    @Autowired
    private ISectorDao sectorDao;

    @Autowired
    private IDispatcherBiz dispatcherBiz;

    @Autowired
    private IUserDao userDao;

    @Transactional
    @Override
    public PoliceInfo add(PoliceInfo policeInfo) {
        List<Sector> sectorList = sectorDao.findAllByInfo(policeInfo.getpInfoServerIp(), policeInfo.getpInfoMainProt(), policeInfo.getpSectorNumber());
        if (ObjectUtils.isEmpty(sectorList)) {
            throw new MyException(ResultEnum.FACILITY_EXISTS);
        }
        sectorList.forEach(sector -> {
            sector.setSectorFlag(Integer.valueOf(policeInfo.getpInfoState()));
            sectorDao.save(sector);
            dispatcherBiz.dispatcher(sector);
        });
//        policeInfo.setpInfoTime(new Timestamp(System.currentTimeMillis()));
        //web推送
        //获取防区ID查出是给谁推送
        long sectorId = sectorList.get(0).getSectorId();
        User user = userDao.findBySectorId(sectorId);
        //如果是手机端报警不向web推送
        if (user != null) {
            policeInfo.setUserId(user.getUserId());
            if (policeInfo.getpInfoDeviceId() != 1) {
                new MyWebSocked().sendMsg(user.getUserId() + "");
            }
        }
        policeInfo.setpInfoTime(new Date());
        return policeInfoDao.save(policeInfo);
    }

    @Override
    public Page<PoliceInfo> pageList(Pageable pageable,Long userid) {
        Page<PoliceInfo> infoPage = policeInfoDao.findByUserId(userid, pageable);

        return infoPage;
    }

    @Override
    public List<MessageBean> findGroupTime(Long userId) {
        List<Object[]> groupTime = policeInfoDao.findGroupTime(userId);
        ArrayList<MessageBean> messageBeans = new ArrayList<>();
        for (int i = 0; i < groupTime.size(); i++) {
            MessageBean messageBean = new MessageBean();
            messageBean.setHour(String.valueOf( groupTime.get(i)[0]));
            messageBean.setCount(Long.valueOf(String.valueOf(groupTime.get(i)[1])));
            messageBeans.add(messageBean);
        }
        return messageBeans;
    }
}
